home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Tele / Pete Johnson / FC LogScan 1.0.2<source>.cpt / StringFunctions Unit.p < prev    next >
Encoding:
Text File  |  1991-10-18  |  8.3 KB  |  283 lines  |  [TEXT/PJMM]

  1. {    StringFunctions Unit                                                                            }
  2. {    By: Jon Wind                                                                                    }
  3. {    CIS: [70167,3444]        GENIE: JPWIND        AMERICA ONLINE: JWIND                }
  4. {                                                                                                    }
  5. {    Intro.                                                                                            }
  6. {    -----                                                                                            }
  7. {    I wrote this unit to provide an easy means of manipulating 'STR ' and 'STR#'            }
  8. {    resources which are stored in an application's resource fork.                            }
  9. {                                                                                                    }
  10. {    This unit is free and may be used however you like.  But please do not redistribute    }
  11. {    modified copies without my permission!                                                    }
  12. {                                                                                                    }
  13. {    Usage                                                                                            }
  14. {    ------                                                                                            }
  15. {                                                                                                    }
  16. {    Most of the procedures in this unit are functions, but they could be easily modified    }
  17. {    to work as procedures if you'd rather not deal with returned values.                    }
  18. {                                                                                                    }
  19. {    Call CreateEmptyStr to create a new, empty 'STR ' or 'STR#' resource.  Then     call    }
  20. {    SetIndString to store a new entry into a 'STR#' resource, or SetStr to store a new    }
  21. {    string into a 'STR ' resource.                                                                }
  22. {                                                                                                    }
  23. {    Call GetIndStr to get a 'STR#' entry or call GetStr to get a 'STR ' entry.  Call            }
  24. {    GetTotalStr to get the total number of entries in a 'STR#' resource.                    }
  25. {                                                                                                    }
  26. {    Call aNum2Str, aStr2Num, Replace, ReplaceAll, Lower, and CapitalizeWords to         }
  27. {    perform some more handy string processing.                                                }
  28. {                                                                                                    }
  29. {    Updates                                                                                        }
  30. {    -------                                                                                        }
  31. {                                                                                                    }
  32. {    7/5/90    :    Added a few more comments, removed a DisposHandle that wasn't        }
  33. {                    needed, and added HNoPurge and HPurge lines.                                }
  34. {                                                                                                    }
  35. {                                                                                                    }
  36. unit StringFunctions;
  37.  
  38. interface
  39.  
  40.  
  41.     function GetTotalStr (theID: Integer): Integer;
  42. {get total number of strings in 'STR#' resource - returns resNotFound if resource not found}
  43.  
  44.     function GetIndStr (theID, index: Integer): Str255;
  45. { GetIndString available as a function }
  46.  
  47.     function GetStr (theID: Integer): Str255;
  48. { GetString available as a function }
  49.  
  50.     function SetIndString (theID, index: Integer; newStr: Str255): OSErr;
  51. { Set 'STR#' resource entry to a specific string }
  52.  
  53.     function SetStr (theID: Integer; newStr: Str255): OSErr;
  54. { Set 'STR ' resource to a specific string }
  55.  
  56.     function CreateEmptyStr (theType: ResType; theID: Integer): OSErr;
  57. { create new, empty 'STR#' or 'STR ' resource - returns result from AddResource }
  58.  
  59.     function aNum2Str (aNum: LongInt): Str255;
  60. { converts a number to a string - NumToString available as a function }
  61.  
  62.     function aStr2Num (NumStr: Str255): LongInt;
  63. { converts a string to a number - StringToNum available as a function }
  64. { Note: won't accurately return numbers if letters are in NumStr }
  65.  
  66.     procedure Replace (var strvar: Str255; oldstr, newstr: Str255);
  67. { replace or delete a portion of a string }
  68.  
  69.     procedure ReplaceAll (var strvar: Str255; oldstr, newstr: Str255);
  70. { replace or delete all occurances of oldstr in string Var }
  71.  
  72.     procedure Lower (var strvar: str255);
  73. { convert a string to lower case including those w/ diacritical marks }
  74.  
  75.     procedure CapitalizeWords (var strvar: str255);
  76. { attempts to capitalize words in a string }
  77.  
  78.  
  79.  
  80. implementation
  81.  
  82.  
  83.  
  84.     function GetTotalStr;{ (theID: Integer): Integer}
  85.         var
  86.             thePtr: ^Integer;
  87.             Hndl: Handle;
  88.     begin
  89.         Hndl := GetResource('STR#', theID);            { use Get1Resource to limit search to current resource fork }
  90.         if Hndl <> nil then
  91.             begin
  92.                 thePtr := Pointer(ord4(hndl^));
  93.                 GetTotalStr := thePtr^;
  94.                 ReleaseResource(Hndl);
  95.             end
  96.         else
  97.             GetTotalStr := resNotFound;
  98.     end;  { of func GetTotalStr }
  99.  
  100.  
  101.     function GetIndStr; {(theID, index: Integer): Str255}
  102.         var
  103.             theString: Str255;
  104.     begin
  105.         GetIndString(theString, theID, index);
  106.         GetIndStr := theString;
  107.     end;  { of func GetIndStr }
  108.  
  109.  
  110.     function GetStr;{ (theID: Integer): Str255}
  111.         var
  112.             S1: StringHandle;
  113.     begin
  114.         S1 := GetString(theID);
  115.         GetStr := S1^^;
  116.     end;  { of func GetStr }
  117.  
  118.  
  119.     function SetIndString; {(theID, index: Integer; newStr: Str255): OSErr}
  120.         var
  121.             offset, place: LongInt;
  122.             Hndl: Handle;
  123.             TotalStrings: ^Integer;
  124.             i, theError: Integer;
  125.             EmptyCh: char;
  126.  
  127.     begin
  128.         EmptyCh := char(0);
  129.         Hndl := GetResource('STR#', theID);            { use Get1Resource to limit search to current resource fork }
  130.         if Hndl <> nil then
  131.             begin
  132.                 HNoPurge(Hndl);
  133.                 TotalStrings := Pointer(ord4(hndl^));
  134.                 if index > TotalStrings^ then            { append string(s) }
  135.                     begin
  136.                         for i := Succ(TotalStrings^) to Pred(index) do
  137.                             place := PtrAndHand(Pointer(Ord4(@EmptyCh) + 1), Hndl, 1);        { append nul to STR# }
  138.                         place := PtrAndHand(Pointer(Ord4(@newStr)), Hndl, Succ(Length(newStr)));    { append string to STR# }
  139.                         TotalStrings^ := index;            { set number of strings to reflect addition(s) }
  140.                     end
  141.                 else            { replace existing string with new string }
  142.                     begin
  143.                         offset := 2;
  144.                         for i := 1 to Pred(index) do        { get character offset of specified 'STR#' entry }
  145.                             offset := offset + Succ(Length(GetIndStr(theID, i)));
  146.                         place := Munger(Hndl, offset, nil, Succ(Length(GetIndStr(theID, index))), Pointer(Ord4(@newStr)), Succ(Length(newStr)));
  147.                     end;
  148.                 ChangedResource(Hndl);
  149.                 theError := ResError;
  150.                 if theError = noErr then
  151.                     WriteResource(Hndl);
  152.                 HPurge(Hndl);
  153.                 ReleaseResource(Hndl);
  154.             end
  155.         else
  156.             theError := resNotFound;
  157.         SetIndString := theError;
  158.     end; {of func SetIndString}
  159.  
  160.  
  161.     function SetStr;{ (theID: Integer; newStr: Str255):OSErr}
  162.         var
  163.             S1: StringHandle;
  164.             theError: Integer;
  165.     begin
  166.         S1 := GetString(theID);
  167.         if Handle(S1) <> nil then
  168.             begin
  169.                 SetString(S1, newStr);
  170.                 ChangedResource(Handle(S1));
  171.                 theError := ResError;
  172.                 if theError = noErr then
  173.                     WriteResource(Handle(S1));
  174.             end
  175.         else
  176.             theError := resNotFound;
  177.         SetStr := theError;
  178.     end;  { of proc SetStr }
  179.  
  180.  
  181.     function CreateEmptyStr; {(theType: ResType; theID: Integer): OSErr}
  182.         var
  183.             Hndl: Handle;
  184.             Amt, theError, Zero: Integer;
  185.     begin
  186.         Zero := 0;
  187.         if theType = 'STR#' then    { pass any other type to create a resource containing a single zero }
  188.             Amt := 2
  189.         else
  190.             Amt := 1;
  191.         Zero := PtrToHand(Pointer(Ord(@Zero)), Hndl, Amt);
  192.         AddResource(Hndl, theType, theID, '');
  193.         theError := ResError;
  194.         if theError = noErr then
  195.             WriteResource(Hndl);
  196.         CreateEmptyStr := theError;
  197.     end;  { of proc CreateEmptyStr }
  198.  
  199.  
  200.     function aNum2Str;{(aNum: LongInt): Str255}
  201.         var
  202.             NumStr: Str255;
  203.     begin
  204.         NumToString(aNum, NumStr);
  205.         aNum2Str := NumStr;
  206.     end;
  207.  
  208.  
  209.     function aStr2Num;{(NumStr: Str255): LongInt}
  210.         var
  211.             aNum: LongInt;
  212.     begin
  213.         StringToNum(NumStr, aNum);
  214.         aStr2Num := aNum
  215.     end;
  216.  
  217.  
  218.     procedure Replace;{(var strvar :  Str255; oldstr,newstr : Str255)}
  219.         var
  220.             location: Integer;
  221.     begin
  222.         location := Pos(oldstr, strvar);
  223.         if location > 0 then
  224.             begin
  225.                 Delete(strvar, location, Length(oldstr));
  226.                 if Length(newstr) > 0 then
  227.                     Insert(newstr, strvar, location);
  228.             end;
  229.     end; { of proc Replace }
  230.  
  231.  
  232.     procedure ReplaceAll;{(var strvar :  Str255; oldstr,newstr : Str255)}
  233.         var
  234.             location: Integer;
  235.     begin
  236.         location := Pos(oldstr, strvar);
  237.         while location > 0 do
  238.             begin
  239.                 Delete(strvar, location, Length(oldstr));
  240.                 if Length(newstr) > 0 then
  241.                     Insert(newstr, strvar, location);
  242.                 location := Pos(oldstr, strvar);
  243.             end;
  244.     end; { of proc ReplaceAll }
  245.  
  246.  
  247.     procedure Lower;{(var strvar : str255)}
  248.         var
  249.             i: Integer;
  250.             LowDiacrits, UprDiacrits: string[29];
  251.     begin
  252.         LowDiacrits := 'äåàãâáæçëèêéïìîíñœöòõôóüùûúÿø';
  253.         UprDiacrits := 'ÄÅÀÃÂÁÆÇËÈÊÉÏÌÎÍÑŒÖÒÕÔÓÜÙÛÚŸØ';
  254.         for i := 1 to Length(strvar) do
  255.             if (strvar[i] >= 'A') and (strvar[i] <= 'Z') then  { "normal" upper case }
  256.                 strvar[i] := Chr(Ord(strvar[i]) + 32)
  257.             else if Pos(strvar[i], UprDiacrits) > 0 then { upper case diacriticals }
  258.                 strvar[i] := LowDiacrits[Pos(strvar[i], UprDiacrits)]
  259.     end; { of proc Lower }
  260.  
  261.  
  262.     procedure CapitalizeWords; {(var strvar: str255)}
  263.         var
  264.             C: Str255;
  265.             i: Integer;
  266.             CapNextWord: Boolean;             { capitalize next word marker }
  267.     begin
  268.         CapNextWord := True;
  269.         for i := 1 to Length(strvar) do
  270.             begin
  271.                 if (Ord(strvar[i]) in [0..32]) then            { word breaks }
  272.                     CapNextWord := True;
  273.                 if CapNextWord and not (strvar[i] in [' ', chr(9), chr(39), '(', '[', '“', '‘', '"']) then
  274.                     begin
  275.                         C := strvar[i];
  276.                         UprString(C, True);        { use toolbox to capitalize beginning of next line }
  277.                         strvar[i] := C[1];
  278.                         CapNextWord := False;    { reset capitalize next word var }
  279.                     end;
  280.             end;
  281.     end; { of proc CapitalizeWords }
  282.  
  283. end.